
class GameMapView extends BaseSpriteView {
    private _lookX: number = 0;
    private _lookY: number = 0;
    private _map: TitleMap
    private _player: egret.DisplayObjectContainer;
    private _debugView: egret.Sprite;

    public constructor(controller: BaseController, parent: egret.DisplayObjectContainer) {
        super(controller, parent)

    }

    public initData(): void {
        this._map = new TitleMap()
        this.addChild(this._map)

        this._debugView = new egret.Sprite()
        this._map.addChildAt(this._debugView, 1000)

        {
            let shp: egret.Shape = new egret.Shape()
            shp.graphics.beginFill(0xff0000, 1)
            shp.graphics.drawCircle(0, 0, 50)
            shp.graphics.endFill()
            this._map.addChildAt(shp, 100)
        }

        console.debug("初始化GameMapView")
        this._map.initMap()

        this.myParent.touchEnabled = true
        this.touchEnabled = false
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this)
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this)
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this)
        this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchEnd, this)

        super.initData()
        this.LookAt(0, 0)
    }

    public LookAt(x: number, y: number): void {
        this._lookX = x
        this._lookY = y
        this._map.LookAt(x, y)
    }

    public SetDebugDisplay(debugDraw: Box2D.Dynamics.b2DebugDraw) {
        //this.addChild(debugDraw)
        //debugDraw.SetSprite(this._view)
        debugDraw.SetSprite(this._debugView)
    }

    public SetMainPlayer(player: egret.DisplayObjectContainer): void {
        this._player = player
        this._map.addChild(this._player)
    }

    public LookAtPlayer(): void {
        if (this._player != null) {
            this.LookAt(this._player.x, this._player.y)
        }
    }

    // Touch相关 =========================================================================================
    private _touchX: number = 0;
    private _touchY: number = 0;
    private _touching: boolean = false;
    private onTouchBegin(evt: egret.TouchEvent) {
        this._touching = true
        this._touchX = evt.stageX
        this._touchY = evt.stageY
    }
    private onTouchMove(evt: egret.TouchEvent) {
        this._lookX += evt.stageX - this._touchX
        this._lookY += evt.stageY - this._touchY
        this.LookAt(this._lookX, this._lookY)
        this._touchX = evt.stageX
        this._touchY = evt.stageY
    }
    private onTouchEnd(evt: egret.TouchEvent) {
        this._touching = false
        this._touchX = 0
        this._touchY = 0
    }
}